package com.zpp.wedding_admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zpp.wedding_admin.entity.Banner;
import com.zpp.wedding_admin.entity.Order;
import com.zpp.wedding_admin.service.IOrderService;
import com.zpp.wedding_admin.utils.LQWUtils;
import com.zpp.wedding_common.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 朱盼盼
 * @since 2022-12-29
 */
@Api(description = "订单操作")
@CrossOrigin
@RestController
@RequestMapping("/weddingadmin/order")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @GetMapping("getAllOrder")
    public R getAllOrder() {
        List<Order> orderList = orderService.list(null);
        return R.ok().data("orderList", orderList);
    }

    @GetMapping("getOrderByUserId/{id}")
    public R getOrderByUserId(@PathVariable Integer id) {
        List<Order> orderList = orderService.getOrderList(id);
        return R.ok().data("orderList", orderList).data("total", orderList.size());
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("getOrderPageVo/{currentPage}/{size}/{id}")
    public R getOrderPageVo(@PathVariable Long currentPage,
                            @PathVariable Long size,
                            @PathVariable Integer id) {
        List<Order> orderList = new ArrayList<>();
        long total = 0;
        if ( id != 0 ) {
            Page<Order> page = new Page<Order>(currentPage, size);
            LambdaQueryWrapper<Order> lqw = LQWUtils.getLqw(Order.class);
            lqw.eq(Order::getUserId, id);
            orderService.page(page, lqw);
            orderList = page.getRecords();
            total = page.getTotal();
        } else {
            Map<String, Object> map = orderService.getByPage(currentPage, size);
            orderList = (List<Order>) map.get("orderList");
            total = (long) map.get("total");
        }
        return R.ok().data("orderList", orderList).data("total", total);
    }

    @ApiOperation(value = "添加一条订单")
        @GetMapping("addOrder/{userId}/{activityId}")
    public R addOrder(@PathVariable Integer userId, @PathVariable Integer activityId) {
        boolean b = orderService.addOrder(userId, activityId);
        if ( b ) return R.ok();
        return R.error();
    }
}
